﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="XmlFile.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：XmlFile.cs
// 创建时间：2014-12-08 18:19
// 修改时间：2014-12-08 18:19
// </summary>
// ---------------------------------------------------------------------------------------
#endregion
namespace WebCommon.IO
{
    using System.IO;
    using System.Xml.Serialization;

    public class XmlFile
    {
        /// <summary>
        /// 序列化XML
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="t">序列号对象</param>
        public static void Serialize<T>(string path, T t)
        {
            var xs = new XmlSerializer(typeof(T));

            using (var fs = new FileStream(path, FileMode.Create))
            {
                xs.Serialize(fs, t);
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static T Deserialize<T>(string path)
        {
            var xs = new XmlSerializer(typeof(T));
            using (var fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                return (T)xs.Deserialize(fs);
            }
        }
    }
}